/**
 * @class ExtGames.Stack
 * @param {Array} An ordered array of cards to initialise this stack with
 * Represents an ordered stack of cards
 */
ExtGames.FlinchReservePile = function(config) {
  var config = config || {};
  
  this.opp = config.opp || 0;
  this.savedItems = [];
    
  ExtGames.FlinchReservePile.superclass.constructor.call(this, config);
    
  this.addEvents(
    /**
     * @event carddropped
     * Fires when a card is dropped on this stack
     * @param {ExtGames.Stack} this The stack the card was dropped on
     * @param {ExtGames.Card} card The card that was dropped
     */
    'carddropped'
  );
  
  this.on('render',      this.initializeDropTarget, this);
  this.on('afterlayout', this.applyClasses,         this);
};

Ext.extend(ExtGames.FlinchReservePile, ExtGames.CardHolder, {
	   
   opp: 0,
	
	
  /**
   * Returns true if the request drop is allowed
   * @param {ExtGames.Card} cardToDrop The card the user wishes to drop onto this stack
   * @return {Boolean} True if this drop is allowed
   */
  dropAllowed: function(cardToDrop) {
	  if (isMyHand(cardToDrop.location.itemId)) {
		  return true;
	  }	  
	  return false;
  },
  
  
  /**
   * Iterates over the cards in this stack, adding CSS classes accordingly.
   * Intended to be attached to the afterlayout event
   */
  applyClasses: function() {
	  	  
    this.items.each(function(c) {
      if (c == this.items.items[this.items.length - 1]) {
        //top card in stack
        c.addClass('x-solitaire-card-right');
        c.removeClass('x-solitaire-card-left');
        c.reveal();
      } else {
        c.addClass('x-solitaire-card-left');
        c.removeClass('x-solitaire-card-right');
      };
      
      c.initializeDragSource();
    }, this);
  },
  
  /**
   * Creates the markup to render this stack
   */
  onRender: function(ct, position) {
	  
	var children = [];
	if (this.opp == 0) {
		children = [
		           {
		           	tag: 'h1',
		           	html: 'Reserve'
		           },
		           {
		             cls:   'x-solitaire-play x-solitaire-play-my',
		             style: 'background: url('+cardUrl+') no-repeat bottom left;'
		           }
		         ];
		
		this.el = ct.createChild({
		      cls: 'x-solitaire-hand-wrapper',
		      style: 'min-width: 72px;',
		      children: children
		    });
	}
	else {
		children = [
           {
             cls:   'x-solitaire-play x-solitaire-play-opp',
             style: 'background: url('+cardUrl+') no-repeat bottom left;'
           }
         ];
		
		this.el = ct.createChild({
		      cls: 'x-solitaire-hand-wrapper',
		      //style: 'min-width: 72px;',
		      children: children
		    });
	}
	
    
    this.stackHolder = this.el.child('.x-solitaire-play');
    
    ExtGames.FlinchReservePile.superclass.onRender.apply(this, arguments);
  },

  getLayoutTarget: function(){
    return this.stackHolder;
  },
  
  didPlay: function() {
	  if (this.items && this.items.items && this.items.items.length > 0) {
		  return true;
	  }
	  return false;
  },
  
  toObject: function() {
	  var o = {};
	  o.cards = [];
	  
	  if (this.items && this.items.items) {
		  for (var i = 0; i < this.items.items.length; i++) {
			  var c = this.items.items[i];
			  o.cards[i] = c.toObject();
		  }
	  }
	  
	  return o;
  }
  
});